<?php 
class Chats extends CI_Model {
	public function __construct() {
		if (!defined("CHAT_TYPE_0")) define("CHAT_TYPE_0", 'Message');
		if (!defined("CHAT_TYPE_1")) define("CHAT_TYPE_1", 'Quote');
		if (!defined("CHAT_TYPE_2")) define("CHAT_TYPE_2", 'Accept');
		if (!defined("CHAT_TYPE_3")) define("CHAT_TYPE_3", 'Confirm');
		if (!defined("CHAT_TYPE_4")) define("CHAT_TYPE_4", 'Payment Request');
		if (!defined("CHAT_TYPE_5")) define("CHAT_TYPE_5", 'Payment');
		if (!defined("CHAT_TYPE_6")) define("CHAT_TYPE_6", 'Issue Invoice');
		if (!defined("CHAT_TYPE_7")) define("CHAT_TYPE_7", 'Job Closed');
		if (!defined("CHAT_TYPE_8")) define("CHAT_TYPE_8", 'Price Change');
		if (!defined("CHAT_TYPE_9")) define("CHAT_TYPE_9", 'Invoice');
		if (!defined("CHAT_TYPE_10")) define("CHAT_TYPE_10", 'Cancellation Fee');
		
		if (!defined("CHAT_PAYMENT_0")) define("CHAT_PAYMENT_0", 'PayPal');
		if (!defined("CHAT_PAYMENT_1")) define("CHAT_PAYMENT_1", 'Bank Transfer');
		if (!defined("CHAT_PAYMENT_2")) define("CHAT_PAYMENT_2", 'Cheque');
	}
	public function getConversation($id) {
		$sql = "SELECT *, DATE_FORMAT(`postdate`, '%e %b %Y, %H:%i') AS `date` FROM `conversation` WHERE `jid` = ".$id." ORDER BY `id` ASC";
		$query = $this->db->query($sql);
		
		return $query->result();
	}
	
	/**
	 * Post
	 * Types:
	 * 0 - Message
	 * 1 - Quote
	 * 2 - Accept
	 * 3 - Confirm
	 * 4 - Payment Request
	 * 5 - Payment
	 * 6 - Issue Invoice
	 * 7 - Job Closed
	 * 8 - Price Change
	 * 9 - Invoice issued
	 * 
	 * Payment type:
	 * 0 - PayPal
	 * 1 - Bank Transfers
	 * 2 - Cheque
	 * 
	 * @param int $jid
	 * @param int $uid
	 * @param string $text
	 * @param int $type
	 */
	public function post($jid, $uid, $text, $type = 0, $paid = 0, $ptype = 0) {
		$data = array(
			'jid' => $jid,
			'uid' => $uid,
			'text' => $text,
			'postdate' => date("Y-m-d H:i:s", time()),
			'type' => $type,
			'paid' => $paid,
			'payment' => $ptype
		);
		
		$this->db->insert('conversation', $data);
	}
	
	public function getSentPrice($jid) {
		$sql = "SELECT * FROM `conversation` WHERE `type` = 5 AND `jid` = ".(int)$jid." ORDER BY `id` DESC LIMIT 1";
		$query = $this->db->query($sql);
		return $query->row();
	}
	
	public function addToCal($jid) {
		$this->load->model('Jobs');
		$this->load->library('Gcal');
		$user =& Users::getInstance();
		
		$jinfo = $this->Jobs->getOrderJob($jid);
		$uinfo = $user->getUserInfo($jinfo->owner_id);
		$jobs = $this->Jobs->getOrderJobs($jid);
		
		foreach ($jobs as $job) {
			$date = explode(" ", $job->start_date);
			
			$fromD = $date[0];
			$fromT = substr($date[1], 0, strlen($date[1])-3);
			
			$endD = $fromD;
			$endT = $fromT;
			
			$cuser = 'bookabuspeople@gmail.com';
			$pass = '!bookabuspeople';
			$cal = 'https://www.google.com/calendar/feeds/he1rnaaml2kimir8koolfs0sdg%40group.calendar.google.com/private/full';
			
			if ($job->job_type == 1) {
				$locs = json_decode($job->locations);
				$location = $locs[0];
			} else {
				$location = $job->from;
			}
			
			$desc = $uinfo->name .": ". $uinfo->phone;
			$desc .= "\n\n". $this->Jobs->formatJobInfo($job);
			if (!empty($jinfo->moreinfo))
				$desc .= "\n\nMore info: ". $jinfo->moreinfo;
			$desc .= "\n\n". site_url() ."joborder/".$job->belongs;
			$desc .= "\n\nUpdated by ". $user->name;
			
			$glogin = $this->gcal->clientLogin($cuser, $pass);
			$eventArray = array('title'     =>  "Job ID: ".$this->Jobs->getID($jinfo),
								'desc'      =>  $desc,
								'where'     =>  $location,
								'startDate' =>  $fromD,
								'startTime' =>  $fromT,
								'endDate'   =>  $endD,
								'endTime'   =>  $endT,
								'tzOffset'  =>  '+08'
								);
			//var_dump($eventArray);
			$this->gcal->createEvent($glogin, $eventArray, $cal);
			//$this->gcal->createEvent($glogin, $eventArray, 'https://www.google.com/calendar/feeds/default/private/full/');
		}
	}
	
	function cancelCal($jid){
		$this->load->model('Jobs');
		$this->load->library('Gcal');
		$user =& Users::getInstance();
		
		$jinfo = $this->Jobs->getOrderJob($jid);
		$title     =  "Job ID: ".$this->Jobs->getID($jinfo);
				
		$cuser = 'bookabuspeople@gmail.com';
		$pass = '!bookabuspeople';
		$cal = 'https://www.google.com/calendar/feeds/he1rnaaml2kimir8koolfs0sdg%40group.calendar.google.com/private/full';
		
		$glogin = $this->gcal->clientLogin($cuser, $pass);	
		
		$this->gcal->delete($glogin,$title , $cal);
		//echo $title;
	}
}
?>